package cagt.tools;

import java.util.AbstractCollection;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;

/**
 * Deck is a read only collection of unique cards. It is the source of all cards
 * in the game.
 * 
 * @author Konrad Sza�kowski
 * 
 */
public class Deck<T extends Card> extends AbstractCollection<T> {

	private HashSet<T> cards = new HashSet<T>();

	/**
	 * Protected constructor which initializes the deck - it checks the uniquity
	 * of cards in the initializer.
	 * 
	 * @param initializer
	 *            Initializing connection.
	 */
	protected Deck(final Collection<? extends T> initializer) {
		for (T init : initializer) {
			for (T card : cards) {
				if (init.equals(card)) {
					throw new IllegalArgumentException("Uniquity violdated.");
				}
			}
			cards.add(init);
		}
	}

	@Override
	public int size() {
		return cards.size();
	}

	@Override
	public Iterator<T> iterator() {
		return new Iterator<T>() {
			private Iterator<T> internal = cards.iterator();

			public void remove() {
				throw new UnsupportedOperationException();
			}

			public T next() {
				return internal.next();
			}

			public boolean hasNext() {
				return internal.hasNext();
			}
		};
	}

}
